from flask import Blueprint, jsonify, request, current_app
import random
from datetime import datetime, timedelta

# 创建蓝图
api = Blueprint('api', __name__)

@api.route('/dashboard/data', methods=['GET'])
def dashboard_data():
    """提供仪表板所需的数据"""
    try:
        # 获取请求参数
        days = int(request.args.get('days', 30))
        
        # 生成日期列表
        end_date = datetime.now()
        start_date = end_date - timedelta(days=days)
        dates = [(start_date + timedelta(days=i)).strftime('%Y-%m-%d') for i in range(days)]
        
        # 生成趋势数据
        trend_data = {
            'total': [random.randint(500, 2000) for _ in range(days)],
            'positive': [random.randint(200, 800) for _ in range(days)],
            'negative': [random.randint(100, 500) for _ in range(days)]
        }
        
        # 情感分析数据
        sentiment_data = {
            'positive': random.randint(5000, 8000),
            'neutral': random.randint(3000, 6000),
            'negative': random.randint(2000, 4000)
        }
        
        # 热门话题
        topics = [
            {'title': '新能源汽车产业发展', 'hot': random.randint(80, 100), 'sentiment': 'positive'},
            {'title': '人工智能技术应用', 'hot': random.randint(70, 95), 'sentiment': 'positive'},
            {'title': '医疗健康产业创新', 'hot': random.randint(60, 90), 'sentiment': 'neutral'},
            {'title': '环保政策实施效果', 'hot': random.randint(50, 85), 'sentiment': 'neutral'},
            {'title': '教育改革争议', 'hot': random.randint(40, 80), 'sentiment': 'negative'}
        ]
        
        # 关键词云
        keywords = [
            {'name': '人工智能', 'value': random.randint(800, 1000)},
            {'name': '大数据', 'value': random.randint(700, 900)},
            {'name': '区块链', 'value': random.randint(600, 800)},
            {'name': '云计算', 'value': random.randint(500, 700)},
            {'name': '物联网', 'value': random.randint(400, 600)},
            {'name': '5G技术', 'value': random.randint(300, 500)},
            {'name': '数字经济', 'value': random.randint(200, 400)},
            {'name': '智慧城市', 'value': random.randint(100, 300)},
            {'name': '网络安全', 'value': random.randint(50, 250)},
            {'name': '元宇宙', 'value': random.randint(30, 200)}
        ]
        
        # 来源分布
        sources = [
            {'name': '微博', 'value': random.randint(300, 500)},
            {'name': '新闻网站', 'value': random.randint(200, 400)},
            {'name': '论坛', 'value': random.randint(150, 350)},
            {'name': '微信', 'value': random.randint(100, 300)},
            {'name': '视频平台', 'value': random.randint(50, 250)}
        ]
        
        # 地区分布
        regions = [
            {'name': '北京', 'value': random.randint(500, 1000)},
            {'name': '上海', 'value': random.randint(400, 900)},
            {'name': '广东', 'value': random.randint(300, 800)},
            {'name': '江苏', 'value': random.randint(200, 700)},
            {'name': '浙江', 'value': random.randint(150, 600)},
            {'name': '四川', 'value': random.randint(100, 500)},
            {'name': '湖北', 'value': random.randint(80, 400)},
            {'name': '河南', 'value': random.randint(60, 300)},
            {'name': '山东', 'value': random.randint(50, 250)},
            {'name': '福建', 'value': random.randint(40, 200)}
        ]
        
        # 统计数据
        stats = {
            'total_comments': random.randint(10000, 50000),
            'total_visits': random.randint(50000, 200000),
            'user_count': random.randint(1000, 5000),
            'alert_count': random.randint(10, 100)
        }
        
        # 组合所有数据
        response_data = {
            'dates': dates,
            'trend': trend_data,
            'sentiment': sentiment_data,
            'topics': topics,
            'keywords': keywords,
            'sources': sources,
            'regions': regions,
            'stats': stats
        }
        
        current_app.logger.info("API: 成功提供仪表板数据")
        return jsonify(response_data)
    
    except Exception as e:
        current_app.logger.error(f"API错误: {str(e)}")
        return jsonify({'error': str(e)}), 500 